<?php
/* parts from http://www.developertutorials.com/tutorials/php/backup-mysql-database-php-050409/page1.html
by Ben Sinclair */

function datadump ($table) {
	$sql = new rex_sql();
	$result = '';
    $result .= "# Dump of $table \n";
    $result .= "# Dump DATE : " . date("d-M-Y") ."\n\n";

    $sql->setQuery("select * from $table");
    $num_fields = $sql->getFields();
	$data = $sql->getArray('',MYSQL_NUM);
	$fieldnames = $sql->getFieldnames();
	
	$fieldString = '(';
	for ($i=0;$i<sizeof($fieldnames);$i++) {
		$fieldString .= "`".$fieldnames[$i]."`";
		if ($i<(sizeof($fieldnames)-1))
			$fieldString .= ", ";
	}
	$fieldString .= ')';
	
    foreach ($data as $row) {
		$result .= "INSERT INTO " . $table . " " . $fieldString . " VALUES(";

		for($j=0; $j<$num_fields; $j++) {
			$row[$j] = addslashes($row[$j]);
			$row[$j] = ereg_replace("\n","\\n",$row[$j]);
			$result .= isset($row[$j]) ? "\"".$row[$j]."\"" : "\"\"";
			if ($j<($num_fields-1)) $result .= ",";
	    }   
		$result .= ");\n";
	}

	return $result . "\n\n\n";
}
?>